package com.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.common.Result;
import com.web.entity.User;
import com.web.entity.UserInfo;
import com.web.mapper.UserMapper;
import com.web.service.UserService;
import com.web.utils.RedisUtil;
import io.swagger.annotations.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.groups.Default;
import java.util.HashMap;
import java.util.List;

@Api(tags = {"用户操作接口"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "用户列表", notes = "分页")
//    @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", paramType = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "用户名", dataType = "String", paramType = "query",dataTypeClass = String.class, example = "张三"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "Integer", paramType = "query", dataTypeClass = Integer.class,example = "18"),
            @ApiImplicitParam(name = "p", value = "页码", dataType = "Integer", paramType = "path", dataTypeClass = Integer.class,example = "1"),
            @ApiImplicitParam(name = "state", value = "状态: 1启用 0禁用", dataType = "Integer", paramType = "query",dataTypeClass = Integer.class, example = "1")
            //@ApiImplicitParam(name = "token", value = "accessToken", dataType = "String", paramType = "header", example = "xxx")
            //@ApiImplicitParam(name = "desc", value = "abcdefg", dataType = "String", paramType = "body", example = "xxx")
    })
    @ApiResponses({
            @ApiResponse(code = 400, message = "请求参数没填好"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
    })
//    @ResponseBody
    @GetMapping("list/{p}")
    public Object List(
            @PathVariable(required = false) int p,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String age,
            @RequestParam(required = false) String state
    ) {

        Page<User> page = new Page<>(p, 3);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "张三"); //查询条件

        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);

        List<User> records = page.getRecords();

        for (User record : records) {
            record.setStateDescp(record.getState());
        }


        HashMap<String, Object> result = new HashMap<>();
        float last_page =  (float) page.getTotal() / 3;
        result.put("total",page.getTotal());
        result.put("current_page",p);
        result.put("pages",userIPage.getPages());
        result.put("last_page",Math.ceil(last_page));
        result.put("per_page",3);
        result.put("list", records);

//        redisTemplate.opsForValue().set("myKey", "myValue");
//        System.out.println(redisTemplate.opsForValue().get("myKey"));
//
//        System.out.println(redisUtil.set("myKey","myValue11111"));
//        System.out.println(redisUtil.get("myKey"));

        return Result.success(result);
    }

    // {
    //    "name":"张三",
    //    "age":21,
    //    "email":"1@qq.com"
    //}
    @PostMapping("/save")
    public Result save(@RequestBody @Validated({Default.class}) User user) {
        return Result.success(user.toString());
    }

    @PostMapping("/save1")
    public Result save1(@RequestParam(required = true)  String name,String age,String email, @Validated User user) {
        return Result.success(user.toString());
    }

    @PostMapping("/save2")
    public Result save2(@RequestParam(required = false) String name,String age,String email, @Validated({User.InsertGroup.class}) User user) {
        return Result.success(user.toString());
    }

    @PostMapping("/save3")
    public Result save3(@RequestParam(required = false) String age,String name,String email, @Validated({User.UpdateGroup.class}) User user) {
        return Result.success(user.toString());
    }


//    @GetMapping("/{id}")
//    public Object info(@PathVariable(name = "id") Long id) {
//        return Result.success("用户id：" + id, id);
//    }
//
    @RequestMapping("info/{id}")
    public Object GetUser(@PathVariable int id) {
        UserInfo userinfo = userService.getInfoById(id);
        //User user = userinfo.getUser();
        return Result.success(userinfo);
//        return userService.Sel(id).toString();
    }

//    @RequestMapping("info2/{id}")
//    public Object GetUser2(@PathVariable int id) {
//        User userInfo = userService.Sel(id);
//        User userInfo2 = userService.Sel2(id);
//
//        return Result.success("ok", userInfo2);
//        // return Result.success("ok",userService.Sel(id));
//    }


}

